package com.gf.help.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gf.common.bean.Page;
import com.gf.help.dao.QuestionDao;
import com.gf.help.dao.ReplyDao;
import com.gf.help.model.Question;
import com.gf.help.service.QuestionService;
@Transactional
@Service("questionService")
public class QuestionServiceImpl implements QuestionService{
	@Autowired
	private QuestionDao questionDao;
	@Autowired
	private ReplyDao replyDao;
	@Override
	public Page getQuestions(int currentPage, int pageSize) {
		Page page=new Page();
		page.setCurpage(currentPage);
		page.setPageSize(pageSize);
		int total=questionDao.queryCount();
		page.setTotal(total);
		List<Question>questions=questionDao.queryQuestions(currentPage, pageSize);
		page.setResults(questions);
		return page;
	}

	@Override
	public Question getQuestion(long id) {
		return questionDao.queryById(id);
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public boolean updateQuestion(Question question) {
		return questionDao.update(question);
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public boolean deleteQuestion(long id) {
		 boolean tag=questionDao.delete(id);
		boolean tag1= replyDao.deleteByQuestionId(id);
		if(tag&&tag1){
			return true;
		}else{
			return false;
		}
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public boolean saveQuestion(Question question) {
		return questionDao.insert(question);
	}

	@Override
	public Page getQuestionsByAskerId(long askerId, int currentPage,
			int pageSize) {
		Page page=new Page();
		page.setCurpage(currentPage);
		page.setPageSize(pageSize);
		int total=questionDao.queryCountQuestionsByAskId(askerId);
		page.setTotal(total);
		List<Question>questions=questionDao.queryQuestionsByAskerId(askerId, currentPage, pageSize);
		page.setResults(questions);
		return page;
	}

	@Override
	public Page getQuestionsByApplIerId(long applierId, int currentPage,
			int pageSize) {
		Page page=new Page();
		page.setCurpage(currentPage);
		page.setPageSize(pageSize);
		int total=questionDao.queryCountQuestionsByApplIerId(applierId);
		page.setTotal(total);
		List<Question>questions=questionDao.queryQuestionsByApplIerId(applierId, currentPage, pageSize);
		page.setResults(questions);
		return page;
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public boolean updataReadCount(long id,int readCount) {
		return questionDao.updateReadCount(id,readCount+1);
	}

}
